package org.dreamwork.jasmine2.restful.client.gui.ui;

import org.dreamwork.jasmine2.restful.APIDefinition;
import org.dreamwork.jasmine2.restful.MethodDefinition;
import org.dreamwork.jasmine2.restful.client.gui.model.CellEditors;
import org.dreamwork.jasmine2.restful.client.gui.model.ParameterTableModel;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2014/12/11
 * Time: 11:47
 */
public class ParameterTable extends JTable {
    private ParameterTableModel model;

/*
    private Collection<String> set = Arrays.asList (
            "boolean",
            "byte", "char", "int", "long",
            "float", "double"
    );
*/
    private static Map<String, TableCellEditor> editors = new HashMap<String, TableCellEditor> ();
    static {

        editors.put ("byte", CellEditors.INTEGER_EDITOR);
        editors.put ("int", CellEditors.INTEGER_EDITOR);
        editors.put ("long", CellEditors.INTEGER_EDITOR);
        editors.put ("java.lang.Byte", CellEditors.INTEGER_EDITOR);
        editors.put ("java.lang.Integer", CellEditors.INTEGER_EDITOR);
        editors.put ("java.lang.Long", CellEditors.INTEGER_EDITOR);
        editors.put (BigInteger.class.getCanonicalName (), CellEditors.INTEGER_EDITOR);

        editors.put ("float", CellEditors.FLOAT_EDITOR);
        editors.put ("double", CellEditors.FLOAT_EDITOR);
        editors.put ("java.lang.Float", CellEditors.FLOAT_EDITOR);
        editors.put ("java.lang.Double", CellEditors.FLOAT_EDITOR);
        editors.put (BigDecimal.class.getCanonicalName (), CellEditors.FLOAT_EDITOR);

        editors.put ("boolean", CellEditors.BOOLEAN_EDITOR);
        editors.put (Boolean.class.getCanonicalName (), CellEditors.BOOLEAN_EDITOR);

        editors.put (Date.class.getCanonicalName (), CellEditors.DATE_EDITOR);
    }

    public ParameterTable (APIDefinition api, MethodDefinition method) {
        this (api, method, false);
    }

    public ParameterTable (APIDefinition api, MethodDefinition method, boolean isContext) {
        model = new ParameterTableModel (api, method, isContext);
        setModel (model);
        putClientProperty ("terminateEditOnFocusLost", Boolean.TRUE);
        setBorder (BorderFactory.createEtchedBorder ());
        setRowHeight (25);


    }

    @Override
    public TableCellEditor getCellEditor (int row, int column) {
        if (column != 2)
            return super.getCellEditor (row, column);

        String type = model.getValueType (row);
        if (editors.containsKey (type)) {
            return editors.get (type);
        }
        return super.getCellEditor (row, column);
    }
}